<?php
	$listfile = "prefix.list";
	$listarray = file($listfile) or die("Could not read prefix.list\n");
	$prefix = $_GET["prefix"];
	$cur_index = 0;
	if(!isset($prefix)) {
		$prefix= $listarray[0];
	}
	while(strcmp(trim($listarray[$cur_index]),trim($prefix)) != 0 && $cur_index < count($listarray)) {
		$cur_index++;
	}
	if (strcmp(trim($listarray[$cur_index]),trim($prefix)) != 0) {
		$cur_index = 0;
		$prefix = $listarray[0];
	}
	if ($cur_index > 0) {
		$prev = $listarray[$cur_index - 1];
	} else {
		$prev = $listarray[count($listarray) - 1];
	}
	if ($cur_index == count($listarray) - 1) {
		$next = $listarray[0];
	} else {
		$next = $listarray[$cur_index + 1];
	}
?>
<html>
	<head>
		<link rel="stylesheet" type="text/css" href="newstyle.css" />
		<!--Load the AJAX API-->
		<script type="text/javascript" src="https://www.google.com/jsapi"></script>
		<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
		<script type="text/javascript">

		google.load('visualization', "1.0", {packages:["imagechart"]});
		google.load('visualization', "1.0", {packages:["corechart"]});
		google.load('visualization', "1.0", {packages:["table"]});

		var dhash = new Object();
		//bacteria, color: #2B9F78 (greenish)
		dhash['Aaeo'] = 'blue';
		dhash['Bant'] = 'blue';
		dhash['Bbur'] = 'blue';
		dhash['Bfra'] = 'blue';
		dhash['Bmel'] = 'blue';
		dhash['Bper'] = 'blue';
		dhash['Cbur'] = 'blue';
		dhash['Cjej'] = 'blue';
		dhash['Ctep'] = 'blue';
		dhash['Dalk'] = 'blue';
		dhash['Drad'] = 'blue';
		dhash['Ecol'] = 'blue';
		dhash['Hpyl'] = 'blue';
		dhash['Mpne'] = 'blue';
		dhash['Mtub'] = 'blue';
		dhash['Nmen'] = 'blue';
		dhash['Nsp'] = 'blue';
		dhash['Nsp.'] = 'blue';
		dhash['Pflu'] = 'blue';
		dhash['Rmet'] = 'blue';
		dhash['Saur'] = 'blue';
		dhash['Spne'] = 'blue';
		dhash['Tmar'] = 'blue';
		dhash['Tthe'] = 'blue';
		dhash['Xfas'] = 'blue';
		dhash['Ypes'] = 'blue';
		//archaea, color: #D55E00 (orangish)
		dhash['Aboo'] = 'red';
		dhash['Aful'] = 'red';
		dhash['Asac'] = 'red';
		dhash['CKor'] = 'red';
		dhash['Dkam'] = 'red';
		dhash['Hbut'] = 'red';
		dhash['Hjeo'] = 'red';
		dhash['Hsal'] = 'red';
		dhash['Hvol'] = 'red';
		dhash['Meve'] = 'red';
		dhash['Mfer'] = 'red';
		dhash['Mhun'] = 'red';
		dhash['Mkan'] = 'red';
		dhash['Mlab'] = 'red';
		dhash['Mmar'] = 'red';
		dhash['Msed'] = 'red';
		dhash['Msta'] = 'red';
		dhash['Mthe'] = 'red';
		dhash['Nequ'] = 'red';
		dhash['Nmar'] = 'red';
		dhash['Pars'] = 'red';
		dhash['Ptor'] = 'red';
		dhash['Ssol'] = 'red';
		dhash['Taci'] = 'red';
		dhash['Tbar'] = 'red';
		dhash['Tpen'] = 'red';
		//eukarya, color: #F0E442 (yellowish)
		dhash['Afum'] = 'green';
		dhash['Agam'] = 'green';
		dhash['Amel'] = 'green';
		dhash['Atha'] = 'green';
		dhash['Bmal'] = 'green';
		dhash['Cele'] = 'green';
		dhash['Cneo'] = 'green';
		dhash['Crei'] = 'green';
		dhash['Ddis'] = 'green';
		dhash['Dmel'] = 'green';
		dhash['Drer'] = 'green';
		dhash['Ecun'] = 'green';
		dhash['Ehis'] = 'green';
		dhash['Ggal'] = 'green';
		dhash['Glam'] = 'green';
		dhash['Hsap'] = 'green';
		dhash['Isca'] = 'green';
		dhash['Lmaj'] = 'green';
		dhash['Mmus'] = 'green';
		dhash['Oana'] = 'green';
		dhash['Pfal'] = 'green';
		dhash['Scer'] = 'green';
		dhash['Spur'] = 'green';
		dhash['Tbru'] = 'green';
		dhash['Tpar'] = 'green';
		dhash['Xlae'] = 'green';
		dhash['Xtro'] = 'green';

		var cumradarjson = '<?php
$radarname = "./cum_radar/" . trim($prefix) . ".radar.jsonc";
$cumstring = preg_replace('/\s+/', '', file_get_contents($radarname, true));
echo ($cumstring);
?>';
		var radarjson = '<?php
$radarname = "./radar/" . trim($prefix) . ".radar.jsonc";
$radstring = preg_replace('/\s+/', '', file_get_contents($radarname, true));
echo ($radstring);
?>';
		var histjson = '<?php
$histname = "./hist/" . trim($prefix) . ".hist.jsonc";
$histstring = preg_replace('/\s+/','',file_get_contents($histname, true));
echo ($histstring);
?>';
		var alnjson = '<?php
$alnname = "./aln/" . trim($prefix) . ".aln.jsonc";
$alnstring = preg_replace('/\s+/','',file_get_contents($alnname, true));
echo ($alnstring);
?>';


		function drawRadar() {
			var data = new google.visualization.DataTable(radarjson);
			var options = {
				'cht':'r',
				'chf':'bg,s,65432100',
				'chm':'B,6393E0CC,0,1.0,5.0|B,D63333CC,1,1.0,5.0',
				'chco':'6393E0CC|D63333CC',
				'chls':'2.0,4.0,0.0|2.0,4.0,0.0',
				'chxr':'0,0.0,360.0',
				'chds':'a',
				'chxt':'x',
			};

			var chart = new google.visualization.ImageChart(document.getElementById('radar_div'));
			chart.draw(data,options);
		}

		function drawCumRadar() {
			var data = new google.visualization.DataTable(cumradarjson);
			var options = {
				'cht':'r',
				'chf':'bg,s,65432100',
				'chm':'B,6393E0CC,0,1.0,5.0|B,D63333CC,1,1.0,5.0',
				'chco':'6393E0CC|D63333CC',
				'chls':'2.0,4.0,0.0|2.0,4.0,0.0',
				'chxr':'0,0.0,360.0',
				'chds':'a',
				'chxt':'x',
			};

			var chart = new google.visualization.ImageChart(document.getElementById('cum_radar_div'));
			chart.draw(data,options);
		}

		function drawRadarTable() {
			var data = new google.visualization.DataTable(radarjson);
			var table = new google.visualization.Table(document.getElementById('table_div'));
			table.draw(data, {width: 200});
		}

		function drawHistTable() {
			var data = new google.visualization.DataTable(histjson);
			var table = new google.visualization.Table(document.getElementById('table_div'));
			table.draw(data, {width: 800});
		}

		function drawAlnTable() {
			var data = new google.visualization.DataTable(alnjson);
			var table = new google.visualization.Table(document.getElementById('table_div'));
			table.draw(data, {width: 800});
		}

		function drawHist() {
			var data = new google.visualization.DataTable(histjson);
			var options = {
				'height':'500',
				'legend':'none',
				'cht':'bvg',
				'chds':'a',
				'chf':'bg,s,65432100',
				'chxt':'x,x,y,y',
				'chxl':'1:|Overlap|3:|Positions',
				'color':'C6D9FD',
				'chm':'F,FF9900,1,0:5,30|H,FF9900,1,0:5,1:30|H,000000,5,0:5,1:30|H,FF9900,4,0:5,1:30',   
				'firstHiddenColumn':'2',
			};

			var chart = new google.visualization.ImageChart(document.getElementById('hist_div'));
			chart.draw(data,options);
		}

		function drawAln() {
			var data = new google.visualization.DataTable(alnjson);
			var max = parseInt(data.getNumberOfColumns()/2 + 1);
			//This might have to be in php
			var series_string = "{";
			var lnum = 0;
			for(i=1;i<data.getNumberOfColumns();i=i+2) {
				if(i!=1) {
					series_string = series_string + ", ";
				}
//				alert(i + ":" + data.getColumnLabel(i) + ":" + dhash[data.getColumnLabel(i).substring(0,4)]);
				series_string = series_string + lnum + ":{color:'" + dhash[data.getColumnLabel(i).substring(0,4)] + "'}";
				lnum++;
			}
			series_string = series_string + "}";
//			alert(series_string);
			var json_series = eval('(' + series_string + ')');
			var options = {
				'height':'500',
				'vAxis': {'maxValue': max},
//				'colors': ['green', 'blue', 'red'],
				'series': json_series,
			};
//			alert(options['series']);

			var chart = new google.visualization.LineChart(document.getElementById('aln_div'));
			chart.draw(data,options);
		}

		google.setOnLoadCallback(drawRadar);
		google.setOnLoadCallback(drawCumRadar);
		google.setOnLoadCallback(drawHist);
		google.setOnLoadCallback(drawAln);
//		google.setOnLoadCallback(drawAlnTable);
		</script>
	</head>
	<body>
		<h1><?php print($prefix) ?></h1>
		<div id="radar_div"></div>
		<div id="cum_radar_div"></div>
		<div id="hist_div"></div>
		<div id="aln_div"></div>
<?php
		print "<a id=prev href=http://sequence.crc.nd.edu/index.php?prefix=$prev>prev</a>\n";
		print "<a id=next href=http://sequence.crc.nd.edu/index.php?prefix=$next>next</a>\n";
?>
		<div id="table_div"></div>
	</body>
</html>
